<?php
/**
 * Install script, it create the tables in the database
 * 
 * @author Matteo Vigoni <mattevigo@gmail.com>
 * @package DBEntity
 * 
 * @version 0.2 - last update 08 nov 2009 - 16:00
 */
 
require_once("config_dbentity.php");
require_once DB;

$db_wrapper = new DB($db_config);

$users_table = $db_wrapper->t_users;
$sessions_table = $db_wrapper->t_sessions;
$test_table = $db_wrapper->prefix() . 'test';

$users_query = "CREATE TABLE `$users_table` (
  					`user_id` mediumint(8) unsigned NOT NULL auto_increment,
  					`user_username` varchar(255) character set utf8 collate utf8_bin NOT NULL,
  					`user_password` varchar(40) character set utf8 collate utf8_bin NOT NULL,
  					`user_email` varchar(255) character set utf8 collate utf8_bin NOT NULL,
  					`user_admin` tinyint(1) unsigned NOT NULL,
  					PRIMARY KEY  (`user_id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;";

$sessions_query = "CREATE TABLE `$sessions_table` (
  					`session_id` varchar(255) NOT NULL,
  					`user_id` varchar(11) NOT NULL,
  					`session_start` int(11) unsigned NOT NULL,
  					`session_time` int(11) unsigned NOT NULL,
  					`session_browser` varchar(255) NOT NULL,
  					`session_ip` varchar(40) NOT NULL,
  					`session_javascript` tinyint(1) NOT NULL,
  					PRIMARY KEY  (`session_id`)
				) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
				
$test_query = "CREATE TABLE `$test_table` (
  				`test_id` int(32) NOT NULL auto_increment,
  				`test_number` int(11) NOT NULL,
  				`test_string` varchar(24) NOT NULL,
  				PRIMARY KEY  (`test_id`)
			) ENGINE=MyISAM  DEFAULT CHARSET=latin1";
				
try{
	$db_wrapper->query($users_query);
	$db_wrapper->query($sessions_query);
	$db_wrapper->query($test_query);
	echo "Database initialization sucess! Remove the install script.";
	
} catch(DBException $e){
	echo "An error occured!.<br />
			Something like: ".$e->getMessage();
}

$db_wrapper->__destruct();
?>